//
//  SearchListController.m
//  谈诗说词
//
//  Created by dllo on 15/6/15.
//  Copyright (c) 2015年 lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SearchListController.h"
#import "DB.h"
#import "PoemModel.h"
#import "PoemDatabase.h"
#import "ListCell.h"
#import "PomeDetailController.h"
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height
@interface SearchListController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation SearchListController
- (void)dealloc
{
    
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
        self.resultArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 0.32 * WIDTH, 0.08 * WIDTH)];
    title.text = @"搜索结果";
    title.font = [UIFont fontWithName:@"-" size:28];
    title.textAlignment = 1;
    self.navigationItem.titleView = title;

    
    UIImageView *background = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [background setImage:[UIImage imageNamed:@"搜索背景.png"]];
    [self.view  addSubview:background];
    
    self.array = [NSMutableArray arrayWithArray:[PoemDatabase selectAll]];
    
    NSLog(@"%ld", self.array.count);
    
    
    for (int i = 0; i < self.array.count; i++) {
        PoemModel *model = [self.array objectAtIndex:i];
        if ([model.name isEqualToString:self.key]) {
            [self.resultArray addObject:model];
        }else if ([model.poet_name isEqualToString:self.key]){
            [self.resultArray addObject:model];
        }else if ([model.dynasty_name isEqualToString:self.key]){
            [self.resultArray addObject:model];
        }
    }
    [self createTable];
    
    
}
- (void)createTable
{
    UITableView *table = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    table.delegate = self;
    table.dataSource = self;
    [table setBackgroundColor:[UIColor clearColor]];
    table.alpha = 0.7;
    [self.view addSubview:table];
    
    
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.resultArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[ListCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];
    }
    [cell setBackgroundColor:[UIColor clearColor]];
    PoemModel *model = [self.resultArray objectAtIndex:indexPath.row];
    cell.title.text = model.name;
    NSArray *array = [model.content componentsSeparatedByString:@"。"];
    NSString *content = [NSString stringWithFormat:@"%@。", [array firstObject]];
    NSDictionary *dic = @{NSFontAttributeName:[UIFont fontWithName:@"X-" size:19]};
    self.rect = [content boundingRectWithSize:CGSizeMake(WIDTH - 0.14 * WIDTH, 1000) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    cell.pome.frame = CGRectMake(0.07 * WIDTH, 0.22 * WIDTH, 0.86 * WIDTH, self.rect.size.height);
    cell.pome.text = content;
    
    cell.author.text = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    
    return cell;
}
#pragma mark - cell自适应高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.rect.size.height + 0.26 * WIDTH;
}

#pragma mark - cell点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PoemModel *model = [self.resultArray objectAtIndex:indexPath.row];
    PomeDetailController *detail = [[PomeDetailController alloc]init];
    detail.model = model;
    detail.pomeTitle = model.name;
    detail.author = [NSString stringWithFormat:@"/%@/%@", model.dynasty_name, model.poet_name];
    detail.pome = model.content;
    CATransition *trans = [CATransition animation];
    [self.navigationController pushViewController:detail animated:NO];
    trans.type = @"rippleEffect";
    trans.duration = 0.8f;
    [self.navigationController.view.layer addAnimation:trans forKey:nil];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
